package cn.stylefeng.guns.modular.system.util;

import cn.stylefeng.guns.modular.system.util.Util;
import java.util.ArrayList;
import java.util.List;

public class Field55Test {

	private static final String LINE_SP = System.getProperty("line.separator");
	//
	public static final byte[] BYTES = new byte[] {
			(byte) 0x9F,
			0x10,
			0x08,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			(byte) 0x9A,
			0x03,
			0x11,
			0x12,
			0x08, //
			(byte) 0x9C, 0x01,
			0x00,
			(byte) 0x9F,
			0x02,
			0x06,
			0x00,
			0x00,
			0x00,
			0x10,
			0x00,
			0x00,
			0x5F,
			0x2A,
			0x02,
			0x01, //
			0x11, 0x71, (byte) 0x81,
			(byte) 0xC8,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00, //
			0x00, 0x00, 0x00, 0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00, //
			0x00, 0x00, 0x00, 0x00, 0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00, //
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00, //
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00, //
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00, //
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00, //
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00, //
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00, //
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00,
			0x00,
			0x00,
			0x00, //
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00,
			0x00,
			0x00, //
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00,
			0x00, //
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00 };

	//
	public static byte[] getTag(int index, byte[] source) {
		byte tag1 = source[index];
		if ((tag1 & 0x1f) == 0x1f) {
			return new byte[] { source[index], source[index + 1] };
		} else {
			return new byte[] { source[index] };
		}
	}

	//
	public static byte[] getLen(int index, byte[] source) {
		byte len1 = source[index];
		if ((len1 & 0x80) == 0x00) {
			return new byte[] { source[index] };
		} else {
			byte len2 = (byte) (len1 & 0x7f);
			len2++;
			byte[] len = new byte[len2];
			System.arraycopy(source, index, len, 0, len2);
			return len;
		}
	}
	
	public static List<TlvData> parseFiled55Data(String hexData){
		byte[] BYTES = Util.hexStringToByteArray(hexData);
		 int index = 0;
			List<TlvData> tlvs = new ArrayList<TlvData>();

		//
		    while (index < BYTES.length) {
		        byte[] tag = Field55Test.getTag(index, BYTES);
		        index += tag.length;
		        byte[] len = Field55Test.getLen(index, BYTES);
		        index += len.length;
		        int iLen;
		        if (len.length == 1) {
		            iLen = len[0];
		        } else {
		            iLen = len[1] < 0 ? len[1] + 256 : len[1];
		        }
		        byte[] val = Field55Test.getValue(index, BYTES, iLen);
		        index += val.length;
//		        Field55Test.printHex(tag);
//		        Field55Test.printHex(len);
//		        Field55Test.printHex(val);
		        
		        
				tlvs.add(new TlvData(Util.BinToHex(tag, 0, tag.length),
						Util.BinToHex(len, 0, len.length), 
						Util.BinToHex(val, 0, val.length)));
		    }
		    
		    return tlvs;
	}
	

	//
	public static byte[] getValue(int index, byte[] source, int len) {
		byte[] value = new byte[len];
		System.arraycopy(source, index, value, 0, len);
		return value;
	}

	private static void printHex(byte[] bytes) {
		String info = "";
		for (int i = 0; i < bytes.length; i++) {
			String s = Integer.toHexString(bytes[i] < 0 ? bytes[i] + 256
					: bytes[i]);
			if (s.length() == 1) {
				s = "0" + s;
			}
			info += s;
			info += " ";
			if ((i + 1) % 16 == 0) {
				info += LINE_SP;
			}
		}
	}

	public static void main(String[] args) throws Exception {
		List<TlvData> tlvs=parseFiled55Data("1F4204106336301F4101131F4601011F5502202F1F4E04323230311F4712B4514617633889482D220120117924792FBF1F5110343531343631373633333838393438321F4D0800004617633889481F5201011F53033F00001F48789F26085B86CAD83DA76AC29F2701809F100706010A03A000009F370494983C799F36020779950500000000009A031807269C01FF9F02060000000010005F2A02FFFF820200009F1A02FFFF9F0306FFFFFFFFFFFF9F3501FF9F1E085465726D696E616C8407A00000000310109F0902008C9F4104000000691F44089876543210980050");
		for(TlvData tlv : tlvs) {
			System.out.println(tlv.getTag());
		}

	}

}
